package com.foodost.cwpstrokes;

import java.io.IOException;
import java.io.InputStream;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.tsz.afinal.FinalDb;


import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class InitWordTask extends AsyncTask<Context, Integer, String> {

    private static final String TAG = InitWordTask.class.getSimpleName();
    private FinalDb fDB;
    private Context mContext;
    @Override
    protected String doInBackground(Context... params) {
        mContext = params[0];
        fDB = new CWPStrokesModel(mContext).getFinalDb(mContext);
        Log.d(TAG, "start time");
        readExcelAndSaveData(mContext);
        Log.d(TAG, "end time");
        Utils.cwpstrokesInitFilish(mContext);
        return "SUCCESS";
    }
    
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
    
    public void readExcelAndSaveData(Context mContext){
        InputStream in;
        try {
            in = mContext.getResources().getAssets().open("ChineseS.xls");
            Workbook book = Workbook.getWorkbook(in);
            Sheet sheet = book.getSheet("ChineseS");
            int rownum = sheet.getRows();//行数
            int cols = sheet.getColumns();//列数
            Log.d(TAG, "rownum = " + rownum);
            Log.d(TAG, "cols = " + cols);
            for(int row = 1; row < rownum; row++){
                Hanzi hanzi = new Hanzi();
                String word = Utils.unicodeToString("\\u" + sheet.getCell(1, row).getContents());
                hanzi.setWord(word);
                hanzi.setStroke(Integer.parseInt(sheet.getCell(2, row).getContents().trim()));
                if(row < 100){
                    hanzi.setGrage(0);
                }else if(row < 1000){
                    hanzi.setGrage(1);  
                }else if(row <2000){
                    hanzi.setGrage(2); 
                }else if(row <3000){
                    hanzi.setGrage(3); 
                }else if(row <5000){
                    hanzi.setGrage(4); 
                }else if(row <7000){
                    hanzi.setGrage(5); 
                }else if(row <10000){
                    hanzi.setGrage(6); 
                }else if(row <12000){
                    hanzi.setGrage(7); 
                }else if(row <15000){
                    hanzi.setGrage(8); 
                }else if(row <18000){
                    hanzi.setGrage(9); 
                }else{
                    hanzi.setGrage(10); 
                }
                hanzi.setShowed(false);
                fDB.save(hanzi);
                if(row == 100){
                    Intent intent = new Intent();
                    intent.setClass(mContext, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            }
            book.close();
        } catch (BiffException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

}
